package cn.exrick.front.controller;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.manager.dto.State;
import cn.exrick.manager.dto.TreeNode;
import cn.exrick.manager.dto.front.AllGoodsResult;
import cn.exrick.manager.pojo.*;
import cn.exrick.manager.service.ContentService;
import cn.exrick.manager.service.ItemCatService;
import cn.exrick.manager.service.NewsService;
import cn.exrick.manager.service.SysSettingService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;

@Controller
@Api(description = "商品页面展示")
public class GoodsController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private SysSettingService sysSettingService;
   /* @Autowired
    private SearchService searchService;*/

    @RequestMapping(value = "/goods/detail")
    @ApiOperation(value = "商品详情")
    public String getGoodsDetail(Long id,Long cid,Model model,HttpSession session){

        TbItem tbItem = contentService.getGoodsDetail(id);
        cid = tbItem.getCid();
        model.addAttribute("goods",tbItem);
        setAttribute(model,session,cid);
        return "goods-detail";
    }

    /*@ResponseBody
    @RequestMapping(value = "/goods/search",method = RequestMethod.GET)
    @ApiOperation(value = "搜索商品ES")
    public Result<SearchResult> searchProduct(@RequestParam(defaultValue = "") String key,
                                              @RequestParam(defaultValue = "1") int page,
                                              @RequestParam(defaultValue = "20") int size,
                                              @RequestParam(defaultValue = "") String sort,
                                              @RequestParam(defaultValue = "-1") int priceGt,
                                              @RequestParam(defaultValue = "-1") int priceLte){

        SearchResult searchResult=searchService.search(key,page,size,sort,priceGt,priceLte);
        return new ResultUtil<SearchResult>().setData(searchResult);
    }*/


   /* @ResponseBody
    @RequestMapping(value = "/goods/quickSearch",produces= "text/plain;charset=UTF-8",method = RequestMethod.GET)
    @ApiOperation(value = "快速搜索")
    public String getQuickSearch(@RequestParam(defaultValue = "") String key){

        return searchService.quickSearch(key);
    }*/

    @RequestMapping(value = "/goods")
   public String toGoods(Model model,Long cid,String search,HttpSession session){
        model.addAttribute("cid",cid);
        model.addAttribute("search",search);
        if(cid != null && cid != 0){
            TbItemCat itemCat = itemCatService.getItemCatById(cid);
            model.addAttribute("categoryName",itemCat.getName());
            if(itemCat.getParentId() != 0){
                TbItemCat parentItemCat = itemCatService.getItemCatById(itemCat.getParentId());
                if(parentItemCat != null){
                    model.addAttribute("parentCategoryName",parentItemCat.getName());
                }
                if(parentItemCat.getParentId() != 0){
                    TbItemCat parentParentItemCat = itemCatService.getItemCatById(parentItemCat.getParentId());
                    if(parentParentItemCat != null){
                        model.addAttribute("parentParentCategoryName",parentParentItemCat.getName());
                    }

                }
            }
        }

        setAttribute(model,session,cid);
        return "goods";
   }


    private void setAttribute(Model model, HttpSession session,Long cid){
        //商品分类
        List<ZTreeNode> categoryList = itemCatService.getItemCatTree(null);
        model.addAttribute("categoryList",categoryList);
        //导航栏
        List<TbNav> navList=contentService.getNavList();
        model.addAttribute("navList",navList);
        //系统设置
        TbSysSetting setting = sysSettingService.getSetting();
        model.addAttribute("sysSetting",setting);
        //资讯
        DataTablesResult result = newsService.getNewsList(0, 0, 5, new TbNews(), "update_time", "desc");
        model.addAttribute("newsList",result.getData());

        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        if(member != null){
            model.addAttribute("member",member);
        }

        if(cid != null){
            List<TreeNode> nodes = itemCatService.getItemCatTreeNodes(null,cid);
            TreeNode treeNode = new TreeNode();
            treeNode.setNodes(nodes);
            State state = new State();
            state.setExpanded(true);
            if(cid == null || cid == 0){
                state.setSelected(true);
            }
            treeNode.setState(state);
            treeNode.setText("所有商品");
            treeNode.setTags(Collections.singletonList("0"));
            model.addAttribute("treeNodes", JSONObject.toJSONString(Collections.singletonList(treeNode)));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/goods/list",method = RequestMethod.POST)
    public AllGoodsResult goodsList(Long cid,
                            @RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "12") int size,
                            @RequestParam(defaultValue = "") String sort,
                            @RequestParam(defaultValue = "") String search,
                            @RequestParam(defaultValue = "-1") int priceGt,
                            @RequestParam(defaultValue = "-1") int priceLte){
        AllGoodsResult goods=contentService.getAllProduct(page,size,sort,cid,search,priceGt,priceLte);
        return goods;
    }
}
